﻿using Helper;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using static System.Net.Mime.MediaTypeNames;

namespace RemoteDesktop
{
    /// <summary>
    /// WindowCmd.xaml 的交互逻辑
    /// </summary>
    public partial class WindowCmd : Window
    {
        string CurrentText = "";
        string currentDirectory = Environment.SystemDirectory;
        MainWindow mMain;
        List<string> Cmds = new List<string>();
        int CurrentCmdsIndex = 0;
        private string fixedContent = "";
        private int fixedContentLength = 9; // 固定内容的长度
        private int currentPosition = 0; // 当前光标位置
        private bool IsSelf = true;
        private bool IsScreenWord = false;
        public WindowCmd(MainWindow mainWindow)
        {
            InitializeComponent();
            Init(mainWindow);
        }

        private void Init(MainWindow mainWindow)
        {
            mMain = mainWindow;
            Textarea.Text = $"{currentDirectory}>";
            fixedContent = Textarea.Text;
            fixedContentLength = Textarea.Text.Length;
            currentPosition = fixedContentLength;
            Textarea.Select(0, currentPosition);
            Textarea.BorderThickness = new Thickness(0);
            CmdRecord.BorderThickness = new Thickness(0);
            //CmdRecord.Text = Desktopduplication.RunCmd("ver");
            Textarea.Margin = new Thickness(0);
            Textarea.Padding = new Thickness(0);
            CmdRecord.Margin = new Thickness(0);
            CmdRecord.Padding = new Thickness(0);
            if (IsSelf)
            {
                CmdRecord.Text = Desktopduplication.RunCmd("ver");
            }
            else
            {
                mMain.sendCommand("ver",false);
            }
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Textarea.Width = Width - 16;
            CmdRecord.Width = Width - 16;
            Textarea.Height = Height - 39;
        }

        private void Textarea_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            // 如果想要防止默认的回车键行为（比如文本框换行），可以添加下面的代码
            //e.Handled = true;
            //PLogs.Info("KeyDown", e.Key.ToString());
            if (e.Key == Key.Enter)
            {
                CurrentText = Textarea.Text.Replace($"{currentDirectory}>","");
                if("exit".Equals(CurrentText) || "quit".Equals(CurrentText))
                {
                    Close();
                }
                else
                {
                    CmdRecord.Text += $"{currentDirectory}>{CurrentText}\n";
                    Cmds.Add(CurrentText);
                    CurrentCmdsIndex = Cmds.Count -1;
                    if (IsSelf)
                    {
                        if (IsScreenWord)
                        {
                            mMain.pageScreen.SetTheWords(CurrentText);
                        }
                        else
                        {
                            CmdRecord.Text += Desktopduplication.RunCmd(CurrentText);
                        }
                    }
                    else
                    {
                        mMain.sendCommand(CurrentText, IsScreenWord);
                    }
                    CurrentText = "";
                    Textarea.Text = $"{currentDirectory}>";
                    Textarea.Select(Textarea.Text.Length, 0);
                }
            }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            //PLogs.Info("KeyUp", e.Key.ToString());
            if (e.Key == Key.Up)
            {
                CurrentCmdsIndex--;
                if (CurrentCmdsIndex < 0)
                {
                    CurrentCmdsIndex = 0;
                }
                if (Cmds.Count > 0 && CurrentCmdsIndex < Cmds.Count)
                {
                    Textarea.Text = $"{currentDirectory}>{Cmds[CurrentCmdsIndex]}";
                    Textarea.Select(Textarea.Text.Length, 0);
                }
            }
            else if (e.Key == Key.Down)
            {
                CurrentCmdsIndex++;
                if (CurrentCmdsIndex > Cmds.Count - 1)
                {
                    CurrentCmdsIndex = Cmds.Count - 1;
                }
                if (Cmds.Count > 0 && Cmds.Count > CurrentCmdsIndex)
                {
                    Textarea.Text = $"{currentDirectory}>{Cmds[CurrentCmdsIndex]}";
                    Textarea.Select(Textarea.Text.Length, 0);
                }
            }else if (e.Key == Key.LeftShift)
            {
                IsScreenWord = !IsScreenWord;
            }
            else if (e.Key == Key.LeftCtrl)
            {
                IsSelf = !IsSelf;
            }
        }

        internal void UpdateInfomation(string tag)
        {
            Dispatcher.Invoke(new Action(() =>
            {
                CmdRecord.Text += tag;
                CurrentText = "";
                //Textarea.Text = $"{currentDirectory}>";
                //Textarea.Select(Textarea.Text.Length, 0);
            }));
        }

        private void Textarea_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            //PLogs.Info("TextChanged", Textarea.Text);
            if (!Textarea.Text.StartsWith(fixedContent))
            {
                string msg = Textarea.Text;
                if (msg.Length > 0)
                {

                }
                Textarea.Text = $"{currentDirectory}>";
                Textarea.Select(Textarea.Text.Length, 0);
            }
        }
    }
}
